package jmu.edu.yjc.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jmu.edu.yjc.Utils.BeanCopyUtil;
import jmu.edu.yjc.Utils.WebUtils;
import jmu.edu.yjc.config.CustomCellWriteHeightConfig;
import jmu.edu.yjc.config.CustomCellWriteWeightConfig;
import jmu.edu.yjc.constants.SystemConstants;
import jmu.edu.yjc.domain.ResponseResult;
import jmu.edu.yjc.domain.entity.Dept;
import jmu.edu.yjc.domain.entity.Elderly;
import jmu.edu.yjc.domain.entity.Post;
import jmu.edu.yjc.domain.vo.PageVo;
import jmu.edu.yjc.domain.vo.dept.DeptVo;
import jmu.edu.yjc.domain.vo.elderly.ExcelElderlyVo;
import jmu.edu.yjc.enums.AppHttpCodeEnum;
import jmu.edu.yjc.interfaces.SaveInterface;
import jmu.edu.yjc.listener.MyExcelListener;
import jmu.edu.yjc.mapper.DeptMapper;
import jmu.edu.yjc.service.DeptService;
import jmu.edu.yjc.service.PostService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 职工表(Dept)表服务实现类
 *
 * @author makejava
 * @since 2023-02-09 23:57:30
 */
@Service("deptService")
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {
    @Resource
    private PostService postService;
    @Override
    public ResponseResult getDeptList(Integer pageNum, Integer pageSize, String name, String phoneNumber) {
        LambdaUpdateWrapper<Dept> wrapper = new LambdaUpdateWrapper<>();
        wrapper.like(StringUtils.hasText(name),Dept::getDeptName,name);
        wrapper.like(StringUtils.hasText(phoneNumber),Dept::getPhonenumber,phoneNumber);
        Page<Dept> page = new Page<>(pageNum,pageSize);
        page(page,wrapper);
        List<DeptVo> deptVos = BeanCopyUtil.copyBeanList(page.getRecords(), DeptVo.class);
        for (DeptVo vo : deptVos) {
            LambdaUpdateWrapper<Post> postwrapper = new LambdaUpdateWrapper<>();
            postwrapper.eq(Post::getPostId,vo.getPostId());
            Post post = postService.getOne(postwrapper);
            vo.setPostName(post.getPostName());
        }
        page(page,wrapper);
        return ResponseResult.okResult(new PageVo(deptVos,page.getTotal()));
    }

    @Override
    public void export(HttpServletResponse response) {
        try {
            //设置请求头
            WebUtils.setDownLoadHeader("职工表.xlsx",response);
            //获取需要导出的数据
            List<Dept> depts = list();
            List<DeptVo> deptVos = BeanCopyUtil.copyBeanList(depts, DeptVo.class);
            for (DeptVo vo : deptVos) {
                LambdaUpdateWrapper<Post> wrapper = new LambdaUpdateWrapper<>();
                wrapper.eq(Post::getPostId,vo.getPostId());
                Post post = postService.getOne(wrapper);
                vo.setPostName(post.getPostName());
            }
            SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd");
            //把数据写入到excel中
            EasyExcel.write(response.getOutputStream(), DeptVo.class).sheet(sfd.format(new Date()))
                    .registerWriteHandler(new CustomCellWriteWeightConfig())
                    .registerWriteHandler(new CustomCellWriteHeightConfig())
                    .doWrite(deptVos);
        } catch (Exception e) {
            //如果出现异常也要响应json数据
            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }

}
